package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.AddAddressBeen;
import cn.xfdzx.android.apps.shop.bean.AddLiveGoodsBeen;
import cn.xfdzx.android.apps.shop.bean.AddressListBeen;
import cn.xfdzx.android.apps.shop.bean.DeleteAddressBeen;
import cn.xfdzx.android.apps.shop.bean.StoreGoodsListBeen;
import cn.xfdzx.android.apps.shop.contract.AddressListContract;
import cn.xfdzx.android.apps.shop.contract.VideoGoodContract;
import cn.xfdzx.android.apps.shop.model.AddressListModel;
import cn.xfdzx.android.apps.shop.model.VideoGoodModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author
 * @date Description：
 */
public class VideoGoodPresenter extends BasePresenter<VideoGoodContract.View> implements VideoGoodContract.Presenter {

    private VideoGoodContract.Model model;

    public VideoGoodPresenter() {
        model = new VideoGoodModel();
    }


    @Override
    public void initStoreGoodsAddress(boolean isRefresh, boolean isLoading, String page, String limit, String keyword) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading) {
            mView.showLoading();
        }
        model.initStoreGoodsData(page, limit, keyword).compose(RxScheduler.<StoreGoodsListBeen>Flo_io_main())
                .as(mView.<StoreGoodsListBeen>bindAutoDispose()).subscribe(new Consumer<StoreGoodsListBeen>() {
            @Override
            public void accept(StoreGoodsListBeen baseObjectBean) throws Exception {
                mView.onInitSuccess(baseObjectBean);
                if (isLoading) {
                    mView.hideLoading();
                }
                if (isRefresh) {
                    mView.finishRefresh();
                } else {
                    mView.finishLoadMore();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onError(throwable);
                if (isLoading) {
                    mView.hideLoading();
                }
                if (isRefresh) {
                    mView.finishRefresh();
                } else {
                    mView.finishLoadMore();
                }
            }
        });
    }

    @Override
    public void addLiveGoods(String live_id, String goods_ids) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.initAddLiveGoodsData(live_id, goods_ids).compose(RxScheduler.<AddLiveGoodsBeen>Flo_io_main())
                .as(mView.<AddLiveGoodsBeen>bindAutoDispose()).subscribe(new Consumer<AddLiveGoodsBeen>() {
            @Override
            public void accept(AddLiveGoodsBeen baseObjectBean) throws Exception {
                mView.onAddLiveGoodsDataSuccess(baseObjectBean);
                mView.hideLoading();
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onError(throwable);
                mView.hideLoading();

            }
        });
    }
}
